<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>出车单添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${basePath!}/genius/layui/css/layui.css" media="all">

</head>
<body>
<div class="layui-form layuiadmin-card-header-auto">
    <div class="layui-form-item">
        <button class="layui-btn layui-btn-primary layuiadmin-btn-admin" data-type="reset"><i
                class="layui-icon layui-icon-refresh"></i>撤销</button>
        <button class="layui-btn layui-btn-warm layuiadmin-btn-admin" data-type="save" data-aaa="123"><i
                class="layui-icon layui-icon-ok"></i>保存</button>
        <button class="layui-btn layui-btn-danger layuiadmin-btn-admin" data-type="back"><i
                class="layui-icon layui-icon-return"></i>返回</button>
    </div>
    <form class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin">
        <!--
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px">
            <legend>基本信息</legend>
        </fieldset> -->
        <!-- estimate_loading_time -->
        <div class="layui-form-item">
            <label class="layui-form-label">装车单编号</label>
            <div class="layui-input-block">
                <input type="text" name="loading_number" id="loading_number" placeholder="请输入" autocomplete="on"
                       class="layui-input" lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">司机选择</label>
            <div class="layui-input-block">
                <select name="driver_select" id="driver_select" lay-search lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">跟车员选择</label>
            <div class="layui-input-block">
                <select name="follower_select" id="follower_select" lay-search lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">同行员选择</label>
            <div class="layui-input-block">
                <div id="test3" class="demo-transfer"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">路线选择</label>
            <div class="layui-input-block">
                <select name="line_select" id="line_select" lay-verify="required">
                </select>
            </div>
        </div>
        <!-- 	<div class="layui-form-item">
            <label class="layui-form-label">答案顺序</label>
            <div class="layui-input-block">
                <input type="text" name="clause_index" id="clause_index" placeholder="请输入" autocomplete="on"
                    class="layui-input">
            </div>
        </div> -->
        <div class="layui-form-item">
            <label class="layui-form-label">车牌号</label>
            <div class="layui-input-block">
                <!-- <input type="text" lay-verify="required" name="car_code" id="car_code" placeholder="请输入"
                    autocomplete="on" class="layui-input">
-->
                <select name="car_code" id="car_code" lay-search lay-verify="required">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">预计装车时间</label>
            <div class="layui-input-block">
                <input type="datetime" lay-verify="required" name="estimate_loading_time"
                       id="estimate_loading_time" placeholder="请输入" autocomplete="on" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">创建者</label>
            <div class="layui-input-block">
                <input type="text" lay-verify="required" readonly name="creater" id="creater" placeholder="请输入"
                       autocomplete="on" class="layui-input" value="默认创建者">
            </div>
        </div>

        <!-- <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                </select>
            </div>
        </div> -->
        <input type="button" class="layui-btn layui-hide layui-btn-fluid" lay-submit lay-filter="add-submit"
               id="add-submit" value="确认">
    </form>
</div>

<script src="${basePath!}/genius/layui/layui.js"></script>
<script>
    const getCookie = (name) => document.cookie.match(`[;\s+]?${name}=([^;]*)`)?.pop();

    let parentJson = {}

    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function setParentJson(json) {
        parentJson = json;
    }

    function backUrl() {
        var bakUrl = parentJson.bakUrl;
        window.location.href = bakUrl;
    }

    layui.config({
        base: '${basePath!}/genius/' //静态资源所在路径
        ,host:'${basePath!}'//项目访问路径
    }).extend({
        setter: 'config'
    }).use(['table', 'form', 'util', 'laydate', 'setter', 'transfer'], function() {
        var $ = layui.$,
            form = layui.form,
            setter = layui.setter,
            util = layui.util,
            laydate = layui.laydate,
            transfer = layui.transfer;

        //获取跟车员
        // $.post('http://localhost:3000/follower/getAllPersonInfo', {
        $.post(setter.reqBase + '/follower/getAllPersonInfo', {
            "type": 'peer'
        }, (res) => {
            let peerData = []
            res.data.forEach(element => {
                peerData.push(element)
            })
            transfer.render({
                elem: '#test3',
                parseData: d => {
                    return {
                        title: d.jobNumber + " " + d.name,
                        value: d.jobNumber,
                        disabled: false, //是否禁用
                        checked: false, //是否选中
                        mobile: d.mobile
                    }
                },
                onchange: (o, i) => {
                    console.log(transfer.getData('tran'));
                },
                data: peerData,
                title: ['可选同行人员', '已选同行人员'],
                showSearch: true,
                height: 250,
                id: 'tran'
            })
        })

        $("#creater").val(getCookie('name'))

        laydate.render({
            elem: '#estimate_loading_time',
            type: 'time'
        });

        parentJson.bakUrl = GetQueryString("bakUrl") || 'http://www.baidu.com';
        parentJson.jsType = GetQueryString("jsType") || 'default';
        setParentJson(parentJson);

        //获取所有线路表
        $.post(setter.reqBase + '/follower/getAllLineCode', {}, (res) => {
            // $.post('http://localhost:3000/follower/getAllLineCode', {}, (res) => {
            res.data.forEach(element => {
                $('#line_select').append(
                    '<option value=' + element[0] + '>' + element[1] +
                    '</option>')
            })
            // 添加完记得render下  否则不会刷新到页面中
            form.render('select');
        })

        //获取司机
        $.post(setter.reqBase + '/follower/getAllPersonInfo', {
            // $.post('http://localhost:3000/follower/getAllPersonInfo', {
            "type": 'driver'
        }, (res) => {
            res.data.forEach(element => {
                $('#driver_select').append(
                    '<option value=' + element.jobNumber + '>' + element.jobNumber +
                    " " +
                    element.name +
                    '</option>')
            })
            // 添加完记得render下  否则不会刷新到页面中
            form.render('select');
        })

        //获取跟车员
        $.post(setter.reqBase + '/follower/getAllPersonInfo', {
            // $.post('http://localhost:3000/follower/getAllPersonInfo', {
            "type": 'follower'
        }, (res) => {
            res.data.forEach(element => {
                $('#follower_select').append(
                    '<option value=' + element.jobNumber + '>' +
                    element.jobNumber + " " + element.name +
                    '</option>')
            })
            // 添加完记得render下  否则不会刷新到页面中
            form.render('select');
        })

        $('.layui-btn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //获取车牌号
        $.post(setter.reqBase + '/follower/getAllCarCode', {
            // $.post('http://localhost:3000/follower/getAllPersonInfo', {
        }, (res) => {
            res.data.forEach(element => {
                $('#car_code').append(
                    '<option value=' + element.carCode + '>' + element.carCode +
                    '</option>')
            })
            // 添加完记得render下  否则不会刷新到页面中
            form.render('select');
        })



        var active = {
            back: function() {
                backUrl();
            },
            save: function() {
                form.on('submit(add-submit)', function(data) {
                    let list = []
                    let d = data.field
                    d.driver_name = $('#driver_select option:selected').text()
                    d.follower_name = $('#follower_select option:selected').text()
                    d.line_name = $('#line_select option:selected').text()
                    d.estimate_loading_time =
                        util.toDateString(new Date(), 'yyyy-MM-dd') +" "+ $('#estimate_loading_time')
                            .val()
                    for (let e in d) {
                        if (d[e] == "") {
                            layer.msg("司机、跟车员、线路、是必须要选择的")
                            return false
                        }
                    }
                    $.post(setter.reqBase + '/follower/insertWaybill', {
                        // $.post('http://localhost:3000/follower/insertWaybill', {
                        // clauseNumber: $('#clause_select').val(),.
                        //表单数据
                        waybill: d,
                        peerData: transfer.getData('tran')
                    }, (res) => {
                        if (res.code == 0) {
                            layer.msg('添加成功', {
                                icon: 1
                            }, function() {
                                backUrl()
                            });
                            // form.render()
                        } else {
                            layer.msg(res.msg);
                        }
                    }).error(e => {
                        layer.msg(e.responseJSON.msg);
                    })

                })
                $('[lay-filter="add-submit"]').trigger('click');
            },
            reset: function() {
                $('[lay-filter="layuiadmin-form-admin"]')[0].reset();
                form.render();
            }
        }

    })
</script>
</body>
</html>
